
from django.views import View
from django.db import transaction

from User.models import OrderVip, OrderGoods, OrderRent
from Venue.models import Vip, Goods, Rent
from sportsground.utils import Req
from sportsground.utils.Res import Res
from sportsground.utils.views import LoginRequiredUser


class RentView(LoginRequiredUser, View):
    """ 租赁订单 """

    @Req.getJson
    def post(self, request):
        """ 添加 """
        rentId = request.json_dict.get('rentId')
        number = request.json_dict.get('number', 1)
        message = request.json_dict.get('message','')

        user = request.user.user

        with transaction.atomic():
            rent = Rent.objects.select_for_update().filter(id=rentId).first()
            if not rent:
                return Res.Json(Res.ParamErr)

            order = OrderRent.create(user=user,rent=rent,number=number, message=message)
            return Res.Json(Res.OK, result=order.Info())
